import * as types from "../types/placeServiceOrderTypes";
import NetWorkUtil from "../utils/NetWorkUtil";
import ApiConstants from "../common/ApiConstants";
import { toastShort } from "../utils/ToastUtils";

export function initOrderService(service_id, facility_id, service_price_id) {
  return dispatch => {
    dispatch({ type: types.PLACEORDER_INIT });
    NetWorkUtil.post(ApiConstants.SERVICEORDER_SIGN, { service_id, facility_id, service_price_id })
      .then(res => {
        const { facility_service } = res;
        NetWorkUtil.post(ApiConstants.SERVICEORDER_INIT, { facility_service })
          .then(data => {
            dispatch({
              type: types.PLACEORDER_DONE,
              data: data.list,
              serviceTypeList: data.serviceTypeList,
              facilitySign: facility_service,
            });
          })
          .catch(error => {
            dispatch({ type: types.PLACEORDER_ERROR, error });
          });
      })
      .catch(error => {
        dispatch({ type: types.PLACEORDER_ERROR, error });
      });
  };
}

export function initOrderUpdate(facility_id, upgrade_lable_id, upgrade_price_id) {
  return dispatch => {
    dispatch({ type: types.PLACEORDER_INIT });
    NetWorkUtil.post(ApiConstants.UPDATEORDER_SIGN, { facility_id, upgrade_lable_id, upgrade_price_id })
      .then(res => {
        const { facility_upgrade } = res;
        NetWorkUtil.post(ApiConstants.UPDATEORDER_INIT, { facility_upgrade })
          .then(data => {
            dispatch({
              type: types.PLACEORDER_DONE,
              data: data.list,
              serviceTypeList: data.upgradeTypeList,
              facilitySign: facility_upgrade,
            });
          })
          .catch(error => {
            dispatch({ type: types.PLACEORDER_ERROR, error });
          });
      })
      .catch(error => {
        dispatch({ type: types.PLACEORDER_ERROR, error });
      });
  };
}

export function palceServiceOrder(facility_service, service_type, service_time, remark, address, callback) {
  return dispatch => {
    if (!address) {
      toastShort("请先设置默认地址");
      return;
    }
    dispatch({ type: types.PLACEORDER_LOADING });
    NetWorkUtil.post(ApiConstants.PLACE_SERVICEORDER, {
      facility_service,
      service_type,
      service_time,
      remark,
      delivery_address_id: address.delivery_address_id,
    })
      .then(data => {
        callback(data.service_order_id);
        dispatch({ type: types.PLACESERVICEORDER_DONE });
      })
      .catch(error => {
        dispatch({ type: types.PLACESERVICEORDER_ERROR, error });
        toastShort("下单失败");
      });
  };
}

export function palceUpdateOrder(facility_upgrade, upgrade_type, upgrade_time, remark, address, callback) {
  return dispatch => {
    if (!address) {
      toastShort("请先设置默认地址");
      return;
    }
    dispatch({ type: types.PLACEORDER_LOADING });
    NetWorkUtil.post(ApiConstants.PLACE_UPDATEORDER, {
      facility_upgrade,
      upgrade_type,
      upgrade_time,
      remark,
      delivery_address_id: address.delivery_address_id,
    })
      .then(data => {
        callback(data.upgrade_order_id);
        dispatch({ type: types.PLACESERVICEORDER_DONE });
      })
      .catch(error => {
        dispatch({ type: types.PLACESERVICEORDER_ERROR, error });
        toastShort("下单失败");
      });
  };
}

export function getDefaultAddress() {
  return dispatch => {
    NetWorkUtil.post(ApiConstants.GET_DEFAULTADDRESS, {})
      .then(address => {
        dispatch({ type: types.PLACEORDER_CHANGE_DEFAULTADDRESS, address });
      })
      .catch(error => {});
  };
}

export function changeServiceWay(newIndex) {
  return dispatch => {
    dispatch({ type: types.PLACEORDER_CHANGE_SERVICEWAY, curServiceWayIndex: newIndex });
  };
}

export function changeDefaultAddress(address) {
  return dispatch => {
    dispatch({ type: types.PLACEORDER_CHANGE_DEFAULTADDRESS, address });
  };
}

export function changeRemark(remark) {
  return dispatch => {
    dispatch({ type: types.PLACEORDER_CHANGE_REMARK, remark });
  };
}

export function changeDatetime(dateTime) {
  return dispatch => {
    dispatch({ type: types.PLACEORDER_CHANGE_DATETIME, dateTime });
  };
}

export function unmount() {
  return dispatch => {
    dispatch({ type: types.PLACEORDER_UNMOUNT });
  };
}
